package _18_剑指OfferII;

import java.util.ArrayList;
import java.util.List;

public class _083_剑指OfferII没有重复元素集合的全排列 {


    public static void main(String[] args) {

        _083_剑指OfferII没有重复元素集合的全排列 v = new _083_剑指OfferII没有重复元素集合的全排列();

        int[] ins = {1, 2, 3};

        List<List<Integer>> permute = v.permute(ins);

        for (List<Integer> integers : permute) {
            System.out.println(integers);
        }

    }

    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {


        dfs(nums, 0, new ArrayList<>());

        return result;
    }

    private void dfs(int[] nums, int index, List<Integer> integers) {
        int len = nums.length;
        if (index == len) {
            result.add(new ArrayList<>(integers));
            return;
        }

        for (int i = index; i < len; i++) {
            // 交换元素
            integers.add(nums[i]);
            swap(nums, index, i);
            dfs(nums, index + 1, integers);
            integers.remove(integers.size() - 1);
            swap(nums, index, i);
        }

    }

    private void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }

}


